<template>
  <div class="sale_data">
    <div class="title">
      <div class="left">
        <h3>销售数据</h3>
        <span>{{ this.timeInfo.start.replace(/-/g, '.') }} ~ {{ this.timeInfo.end.replace(/-/g, '.') }}</span>
      </div>
      <div class="button">
        <span :class="{ active: isActive === `${item.code}` }" v-for="(item, index) in selectTimeType" :key="index" @click="selectDate(item.code)">{{ item.value }}</span>
      </div>
    </div>
    <div class="echart">
      <div class="myechart dataEchartRef" ref="dataEchartRef"></div>
      <div class="myechart scatterEhartRef" v-show="saleLength">
        <div class="myechart scatterEhartRef" ref="scatterEhartRef"></div>
      </div>
      <div class="myechart scatterEhartRef myechartShow" v-show="!saleLength">
        <div class="pic"><img src="~@/assets/images/empty.png" alt="" /></div>
        <p>暂无数据</p>
      </div>
    </div>
  </div>
</template>

<script>
import { amountCollectAPI, regionCollectAPI } from '@/api'
// 引入时间处理器
import { getWeekZh, getCurrentDay, getWeek, getMonth, getyYear, getMonthZh, getYearZh } from '@/plugins/formatDate.js'
export default {
  name: 'SaleEchart',
  data() {
    return {
      // 年月日
      selectTimeType: [
        { value: '周', code: 'week' },
        { value: '月', code: 'month' },
        { value: '年', code: 'year' }
      ],
      // 时间选择样式
      isActive: 'week',
      // 时间
      timeInfo: {
        start: getWeek(),
        end: getCurrentDay()
      },
      // 销售额统计应带参数
      amountCollectInfo: {
        collectType: 1
      },
      // 销售额控制样式所需数据判断
      saleLength: 1,
      // 折线图
      lineOptions: {
        title: {
          text: '销售额趋势图',
          left: 'center'
        },
        grid: {
          left: '1%',
          right: '1%',
          bottom: '1%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis'
        },
        xAxis: {
          type: 'category',
          data: []
        },
        yAxis: {
          name: '单位:元',
          type: 'value'
        },
        series: [
          {
            name: '销售额:',
            data: [],
            type: 'line',
            smooth: true,
            itemStyle: {
              // 图形的形状
              color: '#f55553'
            }
          }
        ]
      },
      // 柱状图
      barOptions: {
        title: {
          text: '销售额分布',
          left: 'center'
        },
        grid: {
          left: '1%',
          right: '1%',
          bottom: '1%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis'
        },
        xAxis: {
          data: [],
          axisLabel: {
            interval: 0 // 横轴信息全部显示
          }
        },
        yAxis: {
          name: '单位:元',
          type: 'value'
        },
        series: [
          {
            name: '销售额',
            type: 'bar',
            data: [],
            itemStyle: {
              // 图形的形状
              color: '#a4c2ff',
              barBorderRadius: [18, 18, 0, 0],
              show: true,
              interval: 0 // 使x轴上的文字显示完全,
            },
            barWidth: 20, // 柱形的宽度
            barCategoryGap: '10%' // 柱形的间距
          }
        ]
      }
    }
  },
  created() {
    this.getEcartsData()
  },
  mounted() {
    this.linechart = this.$echarts.init(this.$refs.dataEchartRef)
    this.chartBox = this.$echarts.init(this.$refs.scatterEhartRef)
  },
  watch: {
    lineOptions: {
      handler() {
        this.lineEcharts()
      },
      deep: true
    },
    barOptions: {
      handler() {
        this.barEcharts()
      },
      deep: true
    }
  },
  methods: {
    // 获取数据
    async getEcartsData() {
      // 获取销售数据统计
      const res1 = await amountCollectAPI({ ...this.amountCollectInfo, ...this.timeInfo })
      // console.log(res1.data.xAxis)
      res1.data.series = res1.data.series.map((item) => (item / 100).toFixed(2))
      if (this.isActive === 'week') {
        res1.data.xAxis = res1.data.xAxis.map((item) => getWeekZh(item))
      } else if (this.isActive === 'month') {
        res1.data.xAxis = res1.data.xAxis.map((item) => getMonthZh(item))
      } else {
        res1.data.xAxis = res1.data.xAxis.map((item) => getYearZh(item))
      }
      this.lineOptions.xAxis.data = res1.data.xAxis
      this.lineOptions.series[0].data = res1.data.series
      // 获取地区汇总销售额数据
      const res2 = await regionCollectAPI(this.timeInfo)
      res2.data.series = res2.data.series.map((item) => (item / 100).toFixed(2))
      // console.log(res2)
      this.saleLength = res2.data.xAxis.length // 控制数据存在显示图标
      this.barOptions.xAxis.data = res2.data.xAxis
      this.barOptions.series[0].data = res2.data.series
    },
    // 折线图
    lineEcharts() {
      this.linechart.setOption(this.lineOptions)
      // 根据页面大小自动响应图表大小
      window.addEventListener('resize', () => {
        this.linechart.resize()
      })
      this.$on('hook:destroyed', () => {
        window.removeEventListener('resize', function () {
          this.linechart.resize()
        })
      })
    },
    // 柱状图
    barEcharts() {
      this.chartBox.setOption(this.barOptions)
      window.addEventListener('resize', () => {
        this.chartBox.resize()
      })
      this.$on('hook:destroyed', () => {
        window.removeEventListener('resize', function () {
          this.chartBox.resize()
        })
      })
    },
    // 年月日事件选择
    selectDate(value) {
      // 年所需类型
      this.amountCollectInfo.collectType = value === 'year' ? 2 : 1
      // 样式
      this.isActive = value
      // 时间
      this.timeInfo.start = value === 'week' ? getWeek() : value === 'year' ? getyYear() : getMonth()
      this.getEcartsData()
    }
  }
}
</script>
<style lang="less" scoped>
.sale_data {
  width: 100%;
  background-color: #fff;
  margin-top: 20px;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 1px 1px 10px rgb(240, 239, 239);
  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .left {
      display: flex;
      align-items: center;
      h3 {
        font-size: 16px;
        color: #333;
      }
      span {
        color: #999;
        font-size: 12px;
        margin-left: 10px;
        height: 20px;
        line-height: 27px;
      }
    }
    .button {
      width: 129px;
      height: 34px;
      background: rgba(233, 243, 255, 0.37);
      border-radius: 10px;
      display: flex;
      justify-content: space-evenly;
      align-items: center;
      span {
        width: 37px;
        height: 25px;
        font-size: 14px;
        color: #9ca3b4;
        cursor: pointer;
        text-align: center;
        line-height: 25px;
      }
      .active {
        background: #fff;
        box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0.2);
        border-radius: 7px;
        font-weight: 600;
        color: #333;
      }
    }
  }
  .echart {
    margin-top: 2px;
    display: flex;
    .myechart {
      flex: 1;
      height: 278px;
    }
    .myechartShow {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .pic {
        width: 123px;
        height: 96px;
      }
      p {
        margin-top: 25px;
        color: #20232a;
        font-size: 14px;
      }
    }
    .scatterEhartRef {
      margin-left: 40px;
    }
  }
}
</style>
